package labox.innovation.gameserver.templates.item;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilderFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.templates.StatsSet;
import labox.innovation.util.Rnd;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * ��ҵ�����Ʒ�����ñ�
 * @author zjj
 *
 */
public class PcItemDropConfig {
	
	static Logger _log = LoggerFactory.getLogger(MobItemCreateConfig.class.getName());
	
	private static final String filename = "data/Stats/Config/PcItemConfig.xml";
	
	private Map<Integer, DropConfigData> _dropData = new HashMap<Integer, DropConfigData>();
	
	public static final PcItemDropConfig getInstance() {
		return SingletonHolder._instance;
	}
	
	
	public void load()
	{
		File _file = new File(Config.DATAPACK_ROOT, filename );
		Document doc;
		
		try {
			
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            factory.setIgnoringComments(true);
            doc = factory.newDocumentBuilder().parse(_file);
            
            Node n = doc.getFirstChild();
            
            if( "list".equalsIgnoreCase( n.getNodeName() ) )
            {
            	NodeList pcstatList = doc.getElementsByTagName("pcstat");
            	
            	for( int i=0; i < pcstatList.getLength() ; i++ )
            	{
            		Node pcNode = pcstatList.item(i);
            		NamedNodeMap pclattrs = pcNode.getAttributes();
            		int imask = Integer.parseInt( pclattrs.getNamedItem("value").getNodeValue() );
            		StatsSet set = new StatsSet();
            		NodeList setList = ( (Element)pcNode ).getElementsByTagName("set");
            		
        			for( int k=0; k<setList.getLength(); k++)
        			{
        				parseBeanSet( setList.item( k ), set );
        			}
        			
        			_dropData.put(imask, new DropConfigData(set) );
        			
            	}// end for( int i=0; i < pcstatList.getLength() ; i++ )
            	
            }
			
		} catch (Exception e) {
            _log.error( "Error loading file " + _file, Config.SERVER_ID, e);
            return ;
		}
		
		
	}// end public void load()
	
	
	/**
	 *  ������ָ��pvp״̬�µ� ����װ����
	 * @param imask
	 * @return
	 */
	public int getDropEquipItemNums( int imask )
	{
		DropConfigData temp = _dropData.get(imask);
		if( temp == null )
		{
			return 0;
		}
		
		return temp.getDropEquipItemNums();
	}
	
	/**
	 *  ������ָ��pvp״̬�µĵ���װ����
	 * @param imask
	 * @return
	 */
	public int getDropItemNums( int imask )
	{
		DropConfigData temp = _dropData.get(imask);
		if( temp == null )
		{
			return 0;
		}
		
		return temp.getDropItemNums();
	}
	
    /**
     * ���� node �����ԣ� ���������ֶμ��뵽 set��ȥ
     * @param node
     * @param set
     */
    protected void parseBeanSet( Node node , StatsSet set )
    {
    	if( node == null )
    	{
    		return;
    	}
        String name = node.getAttributes().getNamedItem("name").getNodeValue().trim();
        String value = node.getAttributes().getNamedItem("val").getNodeValue().trim();
        
        set.set(name, value);
    }
	
	
	/**
	 * ��ɫ����װ���� ����
	 * @author augustus
	 *
	 */
	protected class DropConfigData{
		
		protected int _mustDropEquipItem;  //�ص�װ����
		protected int _mustDropItem;       //�ص��������
		
		protected int[] _RndDropEquipItem; //������װ����
		protected int[] _RndDropEquipItemChance;
		            
		protected int[] _RndDropItem;      //�����䱳�����
		protected int[] _RndDropItemChance;
		
		protected final int MAX_RND_RANGE = 10000;
		
		public DropConfigData( StatsSet set )
		{
			_mustDropEquipItem = set.getInteger("mustDropEquipItem");
			_mustDropItem = set.getInteger("mustDropItem");
			
			//������װ��
			String strRnd = set.getString("RndDropEquipItem");
			String[] rnds = strRnd.split(";");
			_RndDropEquipItem = new int[rnds.length];
			_RndDropEquipItemChance = new int[rnds.length];
			int i=0;
			for( String temp : rnds )
			{
				String[] rndTemp = temp.split(",");
				_RndDropEquipItem[i] = Integer.parseInt( rndTemp[0] );
				_RndDropEquipItemChance[i] = Integer.parseInt( rndTemp[1] );
				i++;
			}
			
			//�����䱳�� ����
			strRnd = set.getString("RndDropItem");
			rnds = strRnd.split(";");
			_RndDropItem = new int[rnds.length];
			_RndDropItemChance = new int[rnds.length];
			i = 0;
			for( String temp : rnds )
			{
				String[] rndTemp = temp.split(",");
				_RndDropItem[i] = Integer.parseInt( rndTemp[0] );
				_RndDropItemChance[i] = Integer.parseInt( rndTemp[1] );

				i++;
			}
			
		}// end public DropConfigData( StatsSet set )
		
		
		/**
		 * ���װ����Ʒ�ĵ�������
		 * @return
		 */
		public int getDropEquipItemNums()
		{
			double fchance = Rnd.nextDouble();
			int chance = (int) (fchance * MAX_RND_RANGE);
			
//			int chance = Rnd.get( MAX_RND_RANGE + 1 );
			int maxchance = 0;
			int minchance = 0;
			for( int i = 0; i < _RndDropEquipItemChance.length; i++ )
			{
				maxchance += _RndDropEquipItemChance[i];
				if( chance >= minchance && chance <= maxchance )
				{
					return _mustDropEquipItem + _RndDropEquipItem[i];
				}
				minchance = maxchance;
			}
			
			return _mustDropEquipItem;
		}
		
		/**
		 *  ��ñ������ĵ�������
		 * @return
		 */
		public int getDropItemNums()
		{
			double fchance = Rnd.nextDouble();
			int chance = (int) (fchance * MAX_RND_RANGE);
			
			int maxchance = 0;
			int minchance = 0;
			for( int i = 0; i < _RndDropEquipItemChance.length; i++ )
			{
				maxchance += _RndDropEquipItemChance[i];
				if( chance >= minchance && chance <= maxchance )
				{
					return _mustDropItem + _RndDropItem[i];
				}
				minchance = maxchance;
			}
			
			return _mustDropItem;
		}
		
		
	}// end class DropConfigData
	
	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder {
		protected static final PcItemDropConfig _instance = new PcItemDropConfig();
	}
}
